package cn.donave.framework.dao;

import java.io.Serializable;
import java.util.List;

import cn.donave.framework.biz.pojos.AbstractPersistent;
import cn.donave.framework.exception.DoNaveException;
import cn.donave.framework.hibernate.HibernateSupport;

public class BaseAnythingDAO extends HibernateSupport implements AnythingDAO {

	private static BaseAnythingDAO baseAnythingDAO = null;

	public BaseAnythingDAO() throws DoNaveException {
		super();
	}

	public static AnythingDAO getInstance() throws DoNaveException {
		if (baseAnythingDAO == null) {
			baseAnythingDAO = new BaseAnythingDAO();
		}
		return baseAnythingDAO;
	}

	public final Serializable insert(AbstractPersistent po)
			throws DoNaveException {
		Serializable id = null;
		try {
			id = super.getHibernateOperate().save(po);
		} catch (DoNaveException e) {
			e.printStackTrace();
		}
		return id;
	}

	public final void update(AbstractPersistent po) throws DoNaveException {
		try {
			super.getHibernateOperate().update(po);
		} catch (DoNaveException e) {
			e.printStackTrace();
		}
	}

	public final void remove(Class clazz, Serializable id)
			throws DoNaveException {
		try {
			super.getHibernateOperate().delete(
					super.getHibernateOperate().load(clazz, id));
		} catch (DoNaveException e) {
			e.printStackTrace();
		}
	}

	public final void remove(AbstractPersistent po) throws DoNaveException {
		try {
			super.getHibernateOperate().delete(po);
		} catch (DoNaveException e) {
			e.printStackTrace();
		}
	}

	public final int remove(String queryString) throws DoNaveException {
		int i = -1;
		try {
			i = super.getHibernateOperate().delete(queryString);
		} catch (DoNaveException e) {
			e.printStackTrace();
		}
		return i;
	}

	public final Object loadObject(AbstractPersistent po)
			throws DoNaveException {
		return this.loadObject(po.getClass(), po.getId());
	}

	public final Object loadObject(Class clazz, Serializable id)
			throws DoNaveException {
		Object obj = null;
		try {
			obj = super.getHibernateOperate().load(clazz, id);
		} catch (DoNaveException e) {
			e.printStackTrace();
		}
		return obj;
	}

	public final List loadAll(AbstractPersistent po) throws DoNaveException {
		return this.loadAll(po.getClass());
	}

	public final List loadAll(Class clazz) throws DoNaveException {
		List list = null;
		try {
			list = super.getHibernateOperate().loadAll(clazz);
		} catch (DoNaveException e) {
			e.printStackTrace();
		}
		return list;
	}

	public Object get(AbstractPersistent po) throws DoNaveException {
		return this.get(po.getClass(), po.getId());
	}

	public Object get(Class clazz, Serializable id) throws DoNaveException {
		Object obj = null;
		try {
			obj = super.getHibernateOperate().get(clazz, id);
		} catch (DoNaveException e) {
			e.printStackTrace();
		}
		return obj;
	}

	public List find(String sqlQuery) throws DoNaveException {
		if (sqlQuery == null || "".equals(sqlQuery))
			return null;
		try {
			return super.getHibernateOperate().find(sqlQuery);
		} catch (DoNaveException e) {
			e.printStackTrace();
		}
		return null;
	}

}
